<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>皮燕子</title>
</head>
<body>
  <canvas class="mycanvas" width="1200" height="800"></canvas>
</body>
<script>
  var mycanvas = document.querySelector('.mycanvas');
  var ctx = mycanvas.getContext('2d');
  ctx.translate(mycanvas.width/2,mycanvas.height/2);

  function drawShape(n,radius){
    //没有输入半径默认100
    var radius = radius||100;

    //保存绘画环境
    ctx.save();

    ctx.beginPath();
    ctx.moveTo(0,-radius);
    
    for(var i = 0;i<n;i++){
      ctx.rotate(2*Math.PI/n);
      ctx.lineTo(0,-radius);
    }

    //还原绘画环境
    ctx.restore();

    ctx.closePath();
    ctx.lineWidth = 4;
    ctx.strokeStyle = "purple";
    ctx.stroke();
  }

  drawShape(6,300);
  drawShape(4,130);
  drawShape(5,200);
  drawShape(7,250);
  drawShape(3);
</script>
</html>